package org.enwey.icarenew.Activity.News;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.Base.BaseActivity;
import org.enwey.icarenew.Adapter.TopicHeaderAdapter;
import org.enwey.icarenew.Component.TopicHeaderView;
import org.enwey.icarenew.Model.Comment;
import org.enwey.icarenew.Model.Topic;
import org.enwey.icarenew.R;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;


/**
 * Created by enwey on 2014/11/23.
 */
public class TopicDetailActivity extends BaseActivity {
    private static final String TAG = "TopicDetailActivity";
    private WebView mWebView;
    private String mTopicId;
    private LinearLayout mSend;
    private ImageView mSupport;
    private ImageView mWeChat;
    private ImageView mQzone;
    private ImageView mSina;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topicdetail);
        setUp();
        findView();
        registerListener();
        CallAPITopicDetail();
    }

    public void setUp() {
        setTitle("新闻详情");
        showLeftBtn();
        mTopicId = getIntent().getStringExtra("topic_id");
        Log.v(TAG, mTopicId);
    }

    private void findView() {
        mSend = (LinearLayout) this.findViewById(R.id.activity_topicdetail_send);
        mSupport = (ImageView) this.findViewById(R.id.activity_topicdetail_support);
        mWeChat = (ImageView) this.findViewById(R.id.activity_topicdetail_wechat);
        mQzone = (ImageView) this.findViewById(R.id.activity_topicdetail_qzone);
        mSina = (ImageView) this.findViewById(R.id.activity_topicdetail_sina);
        mWebView = (WebView) this.findViewById(R.id.activity_topicdetail_web);
        mWebView.setWebViewClient(new MyWebViewClient());
    }

    private void registerListener() {
        View.OnClickListener register = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                if (v == mSend) {
                    intent.setClass(TopicDetailActivity.this, CommentActivity.class);
                    intent.putExtra("newId",mTopicId);
                    ToastUtil.show(TopicDetailActivity.this, "mSend");
                }else if (v == mSupport) {
                    ToastUtil.show(TopicDetailActivity.this, "mSupport");
                }else if (v == mWeChat) {
                    ToastUtil.show(TopicDetailActivity.this, "mWeChat");
                }else if (v == mQzone) {
                    ToastUtil.show(TopicDetailActivity.this, "mQzone");
                }else if (v == mSina) {
                    ToastUtil.show(TopicDetailActivity.this, "mSina");
                }
                startActivity(intent);
            }
        };
        mSend.setOnClickListener(register);
        mSupport.setOnClickListener(register);
        mWeChat.setOnClickListener(register);
        mQzone.setOnClickListener(register);
        mSina.setOnClickListener(register);
    }

    private void CallAPITopicDetail() {
        Hashtable<String, String> parameters = new Hashtable<>();
        parameters.put("topic_id",mTopicId);
        String path = APIManager.getInstance(this).topicDetail(parameters);
        setDetailToWeb(mWebView,path);
    }

    private void setDetailToWeb(WebView mWeb, String path) {
        WebSettings settings = mWeb.getSettings();
        mWeb.loadUrl(path);
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(true);
        mWeb.setBackgroundColor(0);
        Drawable background = mWeb.getBackground();

        if (background != null) {
            background.setAlpha(0);
        }

        mWeb.setWebViewClient(new MyWebViewClient());
    }

    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            showLoading();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            hideLoading();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            hideLoading();
            if (errorCode != 200) {
                ToastUtil.show(TopicDetailActivity.this, "网络异常，请检查网路!");
                return;
            }
        }
    }


}
